﻿namespace AzureCamp.RssAggregatorBusiness
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.ServiceModel.Syndication;

    public class RssAgregatorModel : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
    {
        #region Fields
        private DateTime creationDate;
        private string title;
        private string category;
        private string description;
        private int score;
        private List<string> rssUrls;
        private int subscribers;

        #endregion

        #region Properties
        public DateTime CreationDate
        {
            get { return creationDate; }
        }
        
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        public string Category
        {
            get { return this.category; }
            set { this.category = value; }
        }

        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        public int Score
        {
            get { return this.score; }
            set { this.score = value; }
        }

        public List<string> RssUrls
        {
            get { return this.rssUrls; }
            set { this.rssUrls = value; }
        }

        public int Subscribers
        {
            get { return this.subscribers; }
            set { this.subscribers = value; }
        }

        #endregion

        public RssAgregatorModel()
        {
            this.Title = "unspecified";
            this.Category = "Unknown";
            Init();
        }

        public RssAgregatorModel(string t, string c)
        {
            this.Title = t;
            this.Category = c;
            Init();
        }

        public void AddRssLink(string rssLink)
        {
            this.RssUrls.Add(rssLink);
        }

        public void RemoveRssLink(string rssLink)
        {
            if (this.RssUrls.Contains(rssLink))
            {
                this.RssUrls.Remove(rssLink);
            }
        }

        public void ScoresUp()
        {
            this.Score++;
        }

        public void ScoresDown()
        {
            this.Score--;
        }

        public void SubscribersUp()
        {
            this.Subscribers++;
        }

        public void SubscribersDown()
        {
            this.Subscribers--;
        }

        public Stream GetAgregatorAsStream(string outputFile)
        {
            SyndicationFeed feed = RssHelper.CreateAgregator(this.title,this.description,this.RssUrls, 10);
            RssHelper.SaveSyndicationFeedAsXml(feed, outputFile);

            FileStream stream = new FileStream(outputFile, FileMode.Open);
            return stream;
        }

        private void Init()
        {
            this.RssUrls = new List<string>();
            this.creationDate = DateTime.Now;
            this.Score = 0;
            this.Subscribers = 0;
        }




    }
}
